<template>

  <div style="overflow:hidden">

    <div class="content-zhifu">
      <header class="am-header">
        <h1>微信支付</h1>
      </header>

      <div id="body" style="clear:left">
        <dl class="content">
          <dt>商户订单号：</dt>
          <dd>
            <input readonly="readonly" id="WIDout_trade_no" name="WIDout_trade_no" v-model="WIDout_trade_no" />
          </dd>
          <hr class="one_line">
          <dt>订单名称：</dt>
          <dd>
            <input readonly="readonly" id="WIDsubject" name="WIDsubject" v-model="WIDsubject" />
          </dd>
          <hr class="one_line">
          <dt>付款金额：</dt>
          <dd>
            <input readonly="readonly" id="WIDtotal_amount" name="WIDtotal_amount" v-model="WIDtotal_amount" />
          </dd>
          <hr class="one_line" />
          <dt>商品描述：</dt>
          <dd>
            <input readonly="readonly" id="WIDbody" name="WIDbody" v-model="WIDbody" />
          </dd>
          <hr class="one_line">
          <dt></dt>
          <dd id="btn-dd">
            <span class="new-btn-login-sp">
              <button class="new-btn-login" @click="goPay()" style="text-align:center;">确 认</button>
            </span>
            <span class="note-help">如果您点击“确认”按钮，即表示您同意该次的执行操作。</span>
          </dd>
        </dl>
      </div>

      <div id="foot">
        <ul class="foot-ul">
          <li>
            微信版权所有 2015-2018 WXPAY.COM
          </li>
        </ul>
      </div>
    </div>
  </div>

</template>

<style>
* {
  margin: 0;
  padding: 0;
}
ul,
ol {
  list-style: none;
}
body {
  font-family: "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif;
}
.hidden {
  display: none;
}
.new-btn-login-sp {
  padding: 1px;
  display: inline-block;
  width: 75%;
}
.new-btn-login {
  background-color: #02aaf1;
  color: #ffffff;
  font-weight: bold;
  border: none;
  width: 100%;
  height: 30px;
  border-radius: 5px;
  font-size: 16px;
}
#main {
  width: 100%;
  margin: 0 auto;
  font-size: 14px;
}
.red-star {
  color: #f00;
  width: 10px;
  display: inline-block;
}
.null-star {
  color: #fff;
}
.content {
  margin-top: 10px;
  height: 500px;
}
.content dt {
  width: 100px;
  display: inline-block;
  float: left;
  margin-left: 20px;
  color: #666;
  font-size: 13px;
  margin-top: 8px;
}
.content dd {
  margin-left: 120px;
  margin-bottom: 5px;
}
.content dd input {
  width: 85%;
  height: 28px;
  border: 0;
  -webkit-border-radius: 0;
  -webkit-appearance: none;
}
#foot {
  margin-top: 10px;
  position: absolute;
  bottom: 15px;
  width: 100%;
}
.foot-ul {
  width: 100%;
}
.foot-ul li {
  width: 100%;
  text-align: center;
  color: #666;
}
.note-help {
  color: #999999;
  font-size: 12px;
  line-height: 130%;
  margin-top: 5px;
  width: 100%;
  display: block;
}
#btn-dd {
  margin: 20px;
  text-align: center;
}
.foot-ul {
  width: 100%;
}
.one_line {
  display: block;
  height: 1px;
  border: 0;
  border-top: 1px solid #eeeeee;
  width: 100%;
  margin-left: 20px;
}
.am-header {
  display: -webkit-box;
  display: -ms-flexbox;
  display: box;
  width: 100%;
  position: relative;
  padding: 7px 0;
  -webkit-box-sizing: border-box;
  -ms-box-sizing: border-box;
  box-sizing: border-box;
  background: #1d222d;
  height: 50px;
  text-align: center;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  box-pack: center;
  -webkit-box-align: center;
  -ms-flex-align: center;
  box-align: center;
}
.am-header h1 {
  -webkit-box-flex: 1;
  -ms-flex: 1;
  box-flex: 1;
  line-height: 18px;
  text-align: center;
  font-size: 18px;
  font-weight: 300;
  color: #fff;
}
*,
:before,
:after {
  -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
}

body,
div,
dl,
dt,
dd,
ul,
ol,
li,
h1,
h2,
h3,
h4,
h5,
h6,
form,
fieldset,
legend,
input,
textarea,
p,
blockquote,
th,
td {
  margin: 0;
  padding: 0;
}

table {
  border-collapse: collapse;
  border-spacing: 0;
}

fieldset,
img {
  border: 0;
}

li {
  list-style: none;
}

caption,
th {
  text-align: left;
}

q:before,
q:after {
  content: "";
}

input:password {
  ime-mode: disabled;
}

:focus {
  outline: 0;
}

html,
body {
  -webkit-touch-callout: none;
  touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  tap-highlight-color: transparent;
  height: 100%;
  margin: 0;
  padding: 0;
  text-align: laft;
  font-size: 15px;
  font-weight: 300;
  font-family: "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif;
}

a {
  text-decoration: none;
}

body {
  background: #f4f4f8;
}

.weixin-tip {
  display: none;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  position: absolute;
  top: 15px;
  right: 20px;
  width: 265px;
  padding: 55px 0 0;
  text-align: left;
  background: url();
  background-size: 45px 68px;
}

.weixin-tip-img {
  display: none;
  padding: 110px 0 0;
}

.weixin-tip-img::after {
  display: block;
  margin: 15px auto;
  content: " ";
  background-size: cover;
}

.weixin-tip-img.iphone::after {
  width: 150px;
  height: 150px;
  background-image: url();
}

.weixin-tip-img.android::after {
  width: 173px;
  height: 240px;
  background-image: url();
}
</style>

<script language="javascript">
import { MessageBox } from "mint-ui";
export default {
  data() {
    return {
      WIDout_trade_no: this.GetDateNow(),
      WIDsubject: "",
      WIDtotal_amount: "",
      WIDbody: "",
      mobile: "",
      integral: "",
      type: "",
      orderSn: "",
      amount: "",
      adminId: "",
      mechanismId: "",
      moneyconfig: ""
    };
  },
  mounted() {
    this.GetDateNow(), this.getParams();
  },

  methods: {
    getParams() {
      console.log(this.$route.query);
      this.mobile = this.$route.query.mobile;
      this.integral = this.$route.query.integral;
      this.WIDbody = this.$route.query.reason;
      this.adminId = this.$route.query.adminId;
      this.mechanismId = this.$route.query.mechanismId;
      this.moneyconfig = this.$route.query.ratio;
      this.amount = this.$route.query.amount;
      this.WIDtotal_amount = this.$route.query.money;
      // this.WIDtotal_amount = this.$route.query.money;
      this.WIDsubject = this.$route.query.reason;
      this.type = this.$route.query.type;
    },
    GetDateNow() {
      this.orderSn = this.$route.query.orderSn;
      var vNow = new Date();
      var sNow = "";
      sNow += String(vNow.getFullYear());
      sNow += String(vNow.getMonth() + 1);
      sNow += String(vNow.getDate());
      sNow += String(vNow.getHours());
      sNow += String(vNow.getMinutes());
      sNow += String(vNow.getSeconds());
      sNow += String(vNow.getMilliseconds());
      if (this.orderSn == "" || this.orderSn == undefined) {
        this.WIDout_trade_no = sNow;
      } else {
        this.WIDout_trade_no = this.orderSn;
      }
      // document.getElementById('WIDout_trade_no').value = sNow
      // document.getElementById('WIDsubject').value = '手机网站支付测试商品'
      // document.getElementById('WIDtotal_amount').value = '0.01'
      // document.getElementById('WIDbody').value = '购买测试商品0.01元'
    },
    goPay() {
      //保存充值记录
      this.$http
        .get("mechanismadmin/mobilePay/addWallet", {
          params: {
            mobile: this.mobile,
            integral: this.integral,
            reason: this.WIDbody,
            payType: "2",
            amount: this.amount,
            recordSn: this.WIDout_trade_no,
            adminId: this.adminId,
            mechanismId: this.mechanismId,
            types: this.type
          }
        })
        .then(res => {
          console.log(res.data);
          if (res.data.errno == "-1") {
            MessageBox.alert("该订单已经支付");
            return false;
          }
          //微信支付
          this.$http
            .get("/mechanismadmin/mobilePay/wxpay", {
              params: {
                orderNumber: this.WIDout_trade_no,
                openid: window.sessionStorage.openid,
                money: this.WIDtotal_amount * 100
              }
            })
            .then(
              res => {
                console.log(res.data.returnMap);
                if (res.status == 200) {
                  if (res.data.code == -1) {
                    alert("付款失败");
                    return;
                  }
                  let paySign = res.data.paySign.toString();
                  let nonceStr = res.data.nonce_str.toString();
                  let prepay_id = res.data.prepay_id.toString();
                  let appId = res.data.appid.toString();
                  let timeStamp = res.data.timeStamp.toString();
                  if (typeof WeixinJSBridge == "undefined") {
                    if (document.addEventListener) {
                      document.addEventListener(
                        "WeixinJSBridgeReady",
                        onBridgeReady,
                        false
                      );
                    } else if (document.attachEvent) {
                      document.attachEvent(
                        "WeixinJSBridgeReady",
                        onBridgeReady
                      );
                      document.attachEvent(
                        "onWeixinJSBridgeReady",
                        onBridgeReady
                      );
                    }
                  } else {
                    WeixinJSBridge.invoke(
                      "getBrandWCPayRequest",
                      {
                        appId: appId, //公众号名称，由商户传入
                        timeStamp: timeStamp, //时间戳，自1970年以来的秒数
                        nonceStr: nonceStr, //随机串
                        package: "prepay_id=" + prepay_id,
                        signType: "MD5", //微信签名方式：
                        paySign: paySign //微信签名
                      },
                      function(res) {
                        if (res.err_msg == "get_brand_wcpay_request:ok") {
                          let jumptype = window.sessionStorage.jumptype;
                          if (jumptype == "1") {
                            // this.$router.push({
                            //   path: "/order/" + (status = 2)
                            // });
                            window.location.href = "/order/" + (status = 2);
                          }
                          if (jumptype == "2") {
                            window.history.go(-1);
                          }

                          MessageBox.alert(
                            "你已付款成功！请自行查看积分或订单状态"
                          );
                          // $("#content-box").html(html);
                          // $.toast("支付成功", "text");
                          // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回    ok，但并不保证它绝对可靠。
                        } else {
                          //$.toast(JSON.stringify(res));
                          MessageBox.alert("支付失败");
                        }
                      }
                    );
                  }
                } else {
                  alert("请求出现错误");
                }

                console.log(res);
              },
              err => {
                console.log(err);
              }
            );
        });
    }
  }
};
</script>

</html>

